<?php
class DataRenderer
{
    public function render($data)
    {
        if ($data instanceof SimpleGraph)
        {
            $this->renderGraph($data);
        }
        elseif (is_array($data))
        {
            $this->renderSelectArray($data);
        }
        else
        {
            if (!empty($data))
            {
                echo $data;
            }
        }
    }

    private function renderGraph(SimpleGraph $graph)
    {
        if ($graph->is_empty())
        {
            echo '<p>No data found</p>';
            return;
        }

        $data = $this->getSubjectProperties($graph);
        ?>
        <table summary="Query results">
            <thead>
                <tr>
                    <th>Subject</th>
                    <th>Property</th>
                    <th>Type</th>
                    <th>Value</th>
                    <th colspan="2">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($data as $subject=>$properties) { ?>
                <?php foreach ($properties as $property=>$values) { ?>
                    <?php foreach ($values as $value) { ?>
                        <tr>
                            <td class="nowrap"><?php echo $subject; ?></td>
                            <td class="nowrap"><?php echo $property; ?></td>
                            <td><?php echo $value['type']; ?></td>
                            <td>
                                <?php if ($value['type'] == 'uri') { ?>
                                    <a href="/store/browse?uri=<?php echo urlencode($value['value']); ?>"><?php echo $value['value']; ?></a>
                                <?php } else { ?>
                                    <?php echo $value['value']; ?>
                                <?php } ?>
                            </td>
                            <td><a class="button" href="/store/edit?s=<?php echo urlencode($subject); ?>&amp;p=<?php echo urlencode($property); ?>&amp;o=<?php echo urlencode($value['value']); ?>&amp;type=<?php echo urlencode($value['type']); ?>">Edit</a></td>
                            <td><a class="button" href="/store/delete?s=<?php echo urlencode($subject); ?>&amp;p=<?php echo urlencode($property); ?>&amp;o=<?php echo urlencode($value['value']); ?>&amp;type=<?php echo urlencode($value['type']); ?>">Delete</a></td>
                        </tr>
                    <?php } ?>
                <?php } ?>
            <?php } ?>
            </tbody>
        </table><?php
    }

    private function renderSelectArray(array $data)
    {
        if (!empty($data) && count($data) > 0)
        {
            foreach ($data as $i=>$row)
            {
                if ($i == 0)
                {
                    echo '<table><thead><tr>';
                    foreach ($row as $columnName=>$value)
                    {
                        echo '<th>'.$columnName.'</th>';
                    }
                    echo '</tr></thead><tbody>';
                }

                echo '<tr>';
                foreach ($row as $columnName=>$value)
                {
                    if ($value['type'] == 'uri')
                    {
                        echo '<td><a href="/store/browse?uri='.urlencode($value['value']).'">'.htmlspecialchars($value['value']).'</a></td>';
                    }
                    else
                    {
                        echo '<td>'.$value['value'].'</td>';
                    }
                }
                echo '</tr>';
            }
            echo '</tbody></table>';
        }
        else
        {
            echo '<p>No results found</p>';
        }
    }

    private function getSubjectProperties($graph, $uri=null)
    {
        $data = array();

        if (empty($uri))
        {
            $subjects = $graph->get_subjects();
            sort($subjects);
        }
        else
        {
            $subjects = array($uri);
        }

        foreach ($subjects as $subject)
        {
            $properties = $graph->get_subject_properties($subject);
            sort($properties);

            foreach($properties as $property)
            {
                $values = $graph->get_subject_property_values($subject, $property);
                $data[$subject][$property] = $values;
            }
        }

        return $data;
    }
}
?>